package com.sansecy.base.activity;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import java.util.List;

public class EventDispatchActivity extends AppCompatActivity {

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean handle = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof Window.Callback) {
                handle = ((Window.Callback) fragment).dispatchKeyEvent(event);
            }
        }
        if (!handle) {
            return super.dispatchKeyEvent(event);
        }
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean handle = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof Window.Callback) {
                handle = ((Window.Callback) fragment).dispatchTouchEvent(ev);
            }
        }
        if (!handle) {
            return super.dispatchTouchEvent(ev);
        }
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean handle = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof KeyEvent.Callback) {
                handle = ((KeyEvent.Callback) fragment).onKeyDown(keyCode, event);
            }
        }
        if (!handle) {
            return super.onKeyDown(keyCode, event);
        }
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean handle = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof KeyEvent.Callback) {
                handle = ((KeyEvent.Callback) fragment).onKeyUp(keyCode, event);
            }
        }
        if (!handle) {
            return super.onKeyUp(keyCode, event);
        }
        return true;
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        boolean handle = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof KeyEvent.Callback) {
                handle = ((KeyEvent.Callback) fragment).onKeyLongPress(keyCode, event);
            }
        }
        if (!handle) {
            return super.onKeyLongPress(keyCode, event);
        }
        return true;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        boolean handle = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof KeyEvent.Callback) {
                handle = ((KeyEvent.Callback) fragment).onKeyMultiple(keyCode, repeatCount, event);
            }
        }
        if (!handle) {
            return super.onKeyMultiple(keyCode, repeatCount, event);
        }
        return true;
    }

}
